package com.education.api.controller.admin.education;

import com.education.business.service.education.*;
import com.education.business.service.system.ExamTaskService;
import com.education.common.utils.FileUtil;
import com.education.model.subproject.ExamTask;
import jdk.nashorn.internal.objects.annotations.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.swing.filechooser.FileSystemView;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;

/**
 * 上传下载
 */
@RestController
@RequestMapping("/exam")
public class UpDownloadController {
    @Autowired
    private ExamTaskService examTaskService;
    @Autowired
    private UpDownLoadservice upDownLoadservice;
    /**
     * 文件下载
     * @param response
     * @return
     */
    @GetMapping("/downLoadExamTask")
    public void downLoadFile(HttpServletResponse response,@RequestParam("id")int examTaskId ) {
        String zipFile = upDownLoadservice.downLoadTaskById(response,examTaskId);

        File file = new File(zipFile);

//        file.getAbsoluteFile();
        if (!file.exists()) return;
        System.out.println("文件路径"+file.getAbsoluteFile());
        try

        {
            InputStream inStream = new FileInputStream(file);
            OutputStream os = response.getOutputStream();
//            response.reset();
            //下载后的文件名
            response.setContentType("application/force-download");// 设置强制下载不打开
            response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(file.getName(),"UTF-8"));
            byte[] buff = new byte[1024];
            int len = -1;
            while ((len = inStream.read(buff)) > 0) {
                os.write(buff, 0, len);
            }
            inStream.close();
            os.flush();
        } catch (Exception e) {
            e.printStackTrace();

        }
//        //删除单个文件
//        FileUtil.delete(zipFile);
//        return zipFile;
    }

    @GetMapping("/aa")
    public String aa(){

        //获取桌面路径
        File desktopDir = FileSystemView.getFileSystemView() .getHomeDirectory();
        String desktopPath = desktopDir.getAbsolutePath();



        return desktopPath;
    }
  
}
